<%-- 
    Document   : lab
    Created on : 2019-2-17, 16:53:57
    Author     : dusin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    session.setAttribute("url", "../main/factory.jsp");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>        
        <link rel="stylesheet" href="../../js/jquery/themes/default/easyui.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/icon.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/color.css" type="text/css" />
        <link rel="stylesheet" href="../../css/demo.css" type="text/css" />
        <script type="text/javascript" src="../../js/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="../../js/jquery/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="../../js/jquery/locale/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript">            
            $.extend($.fn.validatebox.defaults.rules, {
                productCode: {//验证废品编码，必须1开头后跟3个数字
                    validator: function(value, param) {
                        return /^[Z|G][a-zA-Z0-9]{3,10}$/.test(value);
                    },
                    message: '请输入正确的货物编码，Z或G开头后跟3-10位数字字母'
                }
            });
            $(document).ready(function() {
                
                $('#dgstudent').datagrid({
                    height: ($(window).height() - 10) + 'px',
                    onClickRow: function(rowIndex, rowData) {
                        //var row = $('#dgdriver').datagrid('getSelected');
                        $('#dgproduct').datagrid('load', {
                            factoryCode: rowData.code
                        });
                    }
                });
                $('#dgproduct').datagrid({
                    height: ($(window).height() - 10) + 'px'
                });
            });
        </script>
    </head>
    <body class="easyui-layout">
        <script type="text/javascript">
            var url; 
            function newStudent() {
                var dd = $('#dgstudent').datagrid('getData')
                $('#dlg').dialog('open').dialog('setTitle', '创建新客户');
                $('#fm').form('clear');
                //$('#studentno1').textbox('readonly',false);                
                url = '../../servlet/web/factory/Save';
            }
            
            function excelImport() {
                $.messager.alert('提示', '该功能等待实现', 'info');
            }
            function destroyStudent() {
                var row = $('#dgstudent').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('提示',
                    '<font color=\'red\'>删除客户信息，和客户相关的信息将全部删除，请谨慎操作！！！</font><br/>您确定要删除<'+row.name+'>信息吗?', function(r) {
                        if (r) {
                            $.post('../../servlet/web/factory/Remove',
                                    {ID: row.ID}, function(result) {
                                if (result.success) {
                                    $('#dgstudent').datagrid('reload');    // reload the user data
                                } else {
                                    $.messager.show({// show error message
                                        title: 'Error',
                                        msg: result.errorMsg
                                    });
                                }
                            }, 'json');
                        }
                    });
                }
            }

            function editStudent() {
                var row = $('#dgstudent').datagrid('getSelected');
                if (row) {
                    $('#dlg').dialog('open').dialog('setTitle', '编辑');
                    $('#fm').form('load', row);
                    url = '../../servlet/web/factory/Update';
                }
            }
            function selectFactory() {
                var row = $('#dgstudent').datagrid('getSelected');
                if (row) {
                    $('#dlgFactory').dialog('open').dialog('setTitle', '编辑');
                    $('#fmFactory').form('load', row);
                    url = '../../servlet/web/factory/UpdateFactory';
                }
            }
            function refreshStudent() {
                $('#dgstudent').datagrid('reload');    // reload the user data
            }
            function saveStudent() {
                //alert(url);
                $('#fm').form('submit', {
                    url: url,
                    onSubmit: function() {
                        return $(this).form('validate');
                    },
                    success: function(result) {
                        var result = eval('(' + result + ')');
                        if (result.errorMsg) {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        } else {
                            $('#dlg').dialog('close');        // close the dialog
                            $('#dgstudent').datagrid('reload');    // reload the user data
                        }
                    }
                });
            }
            function saveFactory() {
                //alert(url);
                $('#fmFactory').form('submit', {
                    url: url,
                    onSubmit: function() {
                        return $(this).form('validate');
                    },
                    success: function(result) {
                        var result = eval('(' + result + ')');
                        if (result.errorMsg) {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        } else {
                            $('#dlgFactory').dialog('close');        // close the dialog
                            $('#dgstudent').datagrid('reload');    // reload the user data
                        }
                    }
                });
            }
            var urlProduct;            

            function newProduct() {
                var dd = $('#dgstudent').datagrid('getSelected');
                if (dd) {
                    $('#fcode').textbox('setText', dd.code);  //此句没有用，因为 下面的.from('clear')将清除所有数据
                    $('#dlgproduct').dialog('open').dialog('setTitle', '创建');
                    $('#productFormTitle').html(dd.name+' 货物类别信息');
                    $('#fmProduct').form('clear');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: '请选择一个客户进行操作'
                    });
                }                
                //$('#productno1').textbox('readonly',false);                
                urlProduct = '../../servlet/web/product/Save';
            }
           
            function destroyProduct() {
                var row = $('#dgproduct').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('提示', "您确定要删除\"<font color=\'red\'>"+row.name+'</font>\"类别吗?', function(r) {
                        if (r) {
                            $.post('../../servlet/web/product/Remove',
                                    {ID: row.ID}, function(result) {
                                if (result.success) {
                                    $('#dgproduct').datagrid('reload');    // reload the user data
                                } else {
                                    $.messager.show({// show error message
                                        title: 'Error',
                                        msg: result.errorMsg
                                    });
                                }
                            }, 'json');
                        }
                    });
                }
            }

            function editProduct() {
                var rowFactory = $('#dgstudent').datagrid('getSelected');
                var row = $('#dgproduct').datagrid('getSelected');
                if (row) {
                    $('#dlgproduct').dialog('open').dialog('setTitle', '编辑');
                    $('#productFormTitle').html(rowFactory.name+' 货物类别信息');
                    $('#fmProduct').form('load', row);
                    urlProduct = '../../servlet/web/product/Update';
                }
            }
            
            function refreshProduct() {
                $('#dgproduct').datagrid('reload');    // reload the user data
            }
            function saveProduct() {
                //alert(urlProduct);
                var dd = $('#dgstudent').datagrid('getSelected');
                $('#fmProduct').form('submit', {
                    url: urlProduct,
                    onSubmit: function(param) {
                        param.factoryCode = dd.code;
                        return $(this).form('validate');
                    },
                    success: function(result) {
                        var result = eval('(' + result + ')');
                        if (result.errorMsg) {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        } else {
                            $('#dlgproduct').dialog('close');        // close the dialog
                            $('#dgproduct').datagrid('reload');    // reload the user data
                        }
                    }
                });
            }
        </script>
        <div region="center" border="true" split="false" style="overflow: hidden; height: 100%;width: 70%">
        <table id="dgstudent" title="重点客户管理" class="easyui-datagrid"
               url="../../servlet/web/factory/Load"
               toolbar="#toolbar"
               rownumbers="true" fitColumns="true" singleSelect="true">
            <thead>
                <tr>                
                    <th field="ID" hidden="true">ID</th>
                    <th field="code" width="60">编号</th>
                    <th field="name" width="200">名称</th>
                    <th field="address" width="200">地址</th>                    
                    <th field="typeName" width="60">类别</th>
                    <th field="plateStart" hidden="true" width="60">车牌前缀</th>                    
                </tr>
            </thead>
        </table>
        <div id="toolbar">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newStudent()">增加</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editStudent()">修改</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyStudent()">删除</a>
            <!--<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="clearStudents()">清除</a>-->
            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refreshStudent()">刷新</a>              
            <!--<a href="#" class="easyui-linkbutton" iconCls="icon-reset" plain="true" onclick="excelImport()">Excel导入</a>-->
        </div>
        </div>
        <div region="east" border="true" split="false" style="overflow: hidden; height: 100%;width:30%;">
            <table id="dgproduct" title="所收货物管理" class="easyui-datagrid"
               url="../../servlet/web/product/LoadByFactory"
               toolbar="#toolbarproduct"
               rownumbers="true" fitColumns="true" singleSelect="true">
            <thead>
                <tr>                   
                    <th field="ID" hidden="true">ID</th>
                    <th field="code" width="100">编码</th>
                    <th field="name" width="120">名称</th>                   
                    <th field="note" width="250">备注</th>
                </tr>
            </thead>
        </table>
        <div id="toolbarproduct">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newProduct()">增加</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editProduct()">修改</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyProduct()">删除</a>            
            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refreshProduct()">刷新</a>            
        </div>
        </div>
        <div id="dlg" class="easyui-dialog" style="width:420px;height:350px;padding:5px"
             closed="true" buttons="#dlg-buttons">
            <div class="ftitle">客户信息</div>
            <form id="fm" method="post"> 
                <div class="fitem" style="display:none" >
                    <label>ID:</label>
                    <input id="ID"  class="easyui-textbox" name="ID"  hidden="true" readonly="true" >
                </div>                
                <div class="fitem" >
                    <label>编码:</label>
                    <input id="code"  class="easyui-textbox" name="code"  required="true">
                </div>
                <div class="fitem">
                    <label>名称:</label>
                    <input id="name" name="name" class="easyui-textbox"   required="true">
                </div>                
                <div class="fitem">
                    <label>地址:</label>
                    <input id="adress" name="address" class="easyui-textbox" data-options="prompt:'请输入正确的地址'" >
                </div>
                <div class="fitem">
                    <label>类别:</label>
                    <select class="easyui-combobox" name="typeCode" id="typeCode" 
                            validType="selectValueRequired['#factoryCode']"
                            style="width:160px"
                            data-options="valueField:'code',textField:'name',                            
                            editable:false,
                            required:true">
                        <option value="G">钢厂</option>
                        <option value="Z">纸厂</option>
                    </select>
                </div>
                <div class="fitem">
                    <label>车牌前缀:</label>
                    <input name="plateStart" class="easyui-textbox">
                </div>
            </form>
        </div>
        <div id="dlg-buttons">
            <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveStudent()">保存</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
        </div>
        <div id="dlgproduct" class="easyui-dialog" style="width:500px;height:320px;padding:5px"
             closed="true" buttons="#dlgproduct-buttons">
            <div class="ftitle" id="productFormTitle">货物类别信息</div>
            <form id="fmProduct" method="post"> 
                <div class="fitem" style="display:none" >
                    <label>ID:</label>
                    <input id="ID"  class="easyui-textbox" name="ID"  hidden="true" readonly="true" >
                </div>
                <div class="fitem"   style="display:none" >
                    <label>fcode:</label>
                    <input id="fcode"  class="easyui-textbox" name="fcode"  readonly="true" >
                </div>
                <div class="fitem" >
                    <label>编码:</label>
                    <input id="code"  class="easyui-textbox" name="code" data-options="prompt:'Z或G开头',validType:'productCode'" required="true">
                </div>
                <div class="fitem">
                    <label>名称:</label>
                    <input id="name" name="name" class="easyui-textbox"   required="true">
                </div>                
                <div class="fitem">
                    <label>备注:</label>
                    <input name="note" >
                </div>
            </form>
        </div>
        <div id="dlgproduct-buttons">
            <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveProduct()">保存</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgproduct').dialog('close')">取消</a>
        </div>
        <style type="text/css">
            #fm{
                margin:0;
                padding:10px 30px;
            }
            #fmProduct{
                margin:0;
                padding:10px 30px;
            }
            .ftitle{
                font-size:14px;
                font-weight:bold;
                padding:5px 0;
                margin-bottom:10px;
                border-bottom:1px solid #ccc;
            }
            .fitem{
                margin-bottom:5px;
            }
            .fitem label{
                display:inline-block;
                width:80px;
            }
            .fitem input{
                width:160px;
            }
        </style>
    </body>
</html>
